using BikyBall.Graphic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BikyBall.Graphic
{
    public static class GraphicEngine
    {
        public static GraphicsDevice Device { get; set; }
        public static ContentManager Content { get; set; }

        public static void RenderToDevice(GraphicProperty graphicProperty, Matrix world, Matrix view, Matrix proj)
        {
            if (graphicProperty.Model == null)
            {
                graphicProperty.Effect.World = world;
                graphicProperty.Effect.View = view;
                graphicProperty.Effect.Projection = proj;
                foreach (EffectPass pass in graphicProperty.Effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    Device.SetVertexBuffer(graphicProperty.VertexBuffer);
                    if (graphicProperty.IndexBuffer != null)
                    {
                        Device.Indices = graphicProperty.IndexBuffer;
                        Device.DrawIndexedPrimitives(graphicProperty.PrimitiveType, 0, 0, graphicProperty.NumVertices, 0, graphicProperty.NumTriangles);
                    }
                    else
                        Device.DrawPrimitives(graphicProperty.PrimitiveType, 0, graphicProperty.NumTriangles);
                }
            }
            else
            {
                graphicProperty.Model.Draw(world, view, proj);
                
                if (graphicProperty.Shader == Shader.TOON_SHADE)
                {
                    Device.RasterizerState = RasterizerState.CullClockwise;
                    graphicProperty.ModelBlack.Draw(Matrix.CreateScale(1.1f) * world, view, proj);
                    Device.RasterizerState = RasterizerState.CullCounterClockwise;
                }
            }
        }
    }
}
